package com.heshuo.basic.dao.impl;


import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.heshuo.basic.dao.IBaseDAO;
import com.heshuo.basic.page.PageUtil;


public class BaseDAOImpl extends HibernateDaoSupport implements IBaseDAO{
	protected Class modelClass = null;
	private static final Logger log = LoggerFactory.getLogger(BaseDAOImpl.class);
	public BaseDAOImpl() {
		
	}

	public BaseDAOImpl(Class modelClass) {
		super();
		this.modelClass = modelClass;
	}

	public int count(String hql) throws Exception {
		log.debug("count instances");
		try {
			Object obj=this.getHibernateTemplate().find("select count(*)"+hql).get(0);
			return ((Long) obj).intValue();
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	public void delete(Object o) throws Exception {
		log.debug("delete instances" + o.toString());
		try {
			this.getHibernateTemplate().delete(o);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void delete(Serializable key) throws Exception {
		try {
			Object obj = findByPrimaryKey(key);
			this.getHibernateTemplate().delete(obj);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List findByPage(final String queryString, PageUtil pageUtil) throws Exception {
		try {
			pageUtil.setTotalRols(this.count(queryString));
			final PageUtil tPageUtil = pageUtil;
			return (List) this.getHibernateTemplate().execute(new HibernateCallback() {
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					Query query = session.createQuery(queryString);
					query.setFirstResult(tPageUtil.getPageStart());
					query.setMaxResults(tPageUtil.getPageSize());
					List list = query.list();
					return list;
				}
			});
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public List findByProperty(String propertyName, Object value) throws Exception {
		String []className =modelClass.getName().split("\\.");
		try {
			String queryString = "from "+className[className.length-1]+  " as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		}
	}
	

	public Object findByPrimaryKey(Serializable key) throws Exception {
		try {
			return this.getHibernateTemplate().get(modelClass, key);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "这里出现了DataAccessException异常";
		}
	}

	public void insert(Object o) throws Exception {
		try {
			System.out.println(o);
			this.getHibernateTemplate().save(o);
		} catch (DataAccessException e) {
			System.out.println("异常DataAccessException");
			e.printStackTrace();
		}
	}

	public void saveOrUpdate(Object o) throws Exception {
		try {
			this.getHibernateTemplate().saveOrUpdate(o);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void update(Object o) throws Exception {
		try {
			this.getHibernateTemplate().update(o);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List findAll(PageUtil pageUtil) throws Exception {
		// TODO Auto-generated method stub
		final String []className =modelClass.getName().split("\\.");
		final String queryString = "from "+className[className.length-1];
		pageUtil.setTotalRols(this.count(queryString));
		final PageUtil tPageUtil = pageUtil;
		try {
			return (List) this.getHibernateTemplate().execute(new HibernateCallback() {
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					
					Query query = session.createQuery(queryString);
					query.setFirstResult(tPageUtil.getPageStart());
					query.setMaxResults(tPageUtil.getPageSize());
					List list = query.list();
					return list;
				}
			});
			
			
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		}
	}

	public List findByProperty(String propertyName, Object value,
			PageUtil pageUtil) throws Exception {
		// TODO Auto-generated method stub
		String []className =modelClass.getName().split("\\.");
		final String queryString = "from "+className[className.length-1]+  " as model where model."
				+ propertyName + "= '" + value.toString() + "' ";
		pageUtil.setTotalRols(this.count(queryString));
		final PageUtil tPageUtil = pageUtil;
		try {
			return (List) this.getHibernateTemplate().execute(new HibernateCallback() {
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					
					Query query = session.createQuery(queryString);
					query.setFirstResult(tPageUtil.getPageStart());
					query.setMaxResults(tPageUtil.getPageSize());
					List list = query.list();
					return list;
				}
			});
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		}
	}

	
	public List findAll() throws Exception {
		// TODO Auto-generated method stub
		String []className =modelClass.getName().split("\\.");
		final String queryString = "from "+className[className.length-1];
		
		try {
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		}
	}

	
	public List findByPage(final String queryString) throws Exception {
		// TODO Auto-generated method stub
		try {
			
			return (List) this.getHibernateTemplate().execute(new HibernateCallback() {
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					Query query = session.createQuery(queryString);
					List list = query.list();
					return list;
				}
			});
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public List findByObject(Object o) throws Exception {
		// TODO Auto-generated method stub
		try {
			return getHibernateTemplate().findByExample(o);
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		}
	}
}
